package com.erp.module.crm.xXCrm

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.SeekActivity
import com.erp.activityKView.CrmFKView
import com.erp.bean.ClientDataBean
import com.erp.myAdapter.ArrayAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.popupwin.PopShejiXiaoshouDuoxuan
import com.erp.util.Httputil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.fragment_clientone.*
import org.json.JSONArray
import org.json.JSONObject

/**
 * Created by Administrator on 2016/11/2 0002.
 * 客户资料
 */
@SuppressLint("ValidFragment")
class FragmentClientone : BaseKFragment, View.OnClickListener {
    lateinit var clientDataBean: ClientDataBean ;
    lateinit var FKP : CrmFKView;
    val kehuleibie = ArrayList<String>()
    var code: Int = 0
    constructor(){
    }
    constructor( clientDataBean: ClientDataBean, FKP: CrmFKView, code: Int){
        this.clientDataBean=clientDataBean
        this.FKP = FKP
        this.code=code
    }
    private val f = "FragmentClientfour"
    lateinit var updatedialog: AlertDialog
    lateinit var mxuanzhongid : MutableList<String>
    lateinit var mjingliid : MutableList<String>
    lateinit var updata: CrmFKView
    override fun getLayoutId(): Int {
        fragmentname = f
        isVisibl = true;
        return R.layout.fragment_clientone
    }

    fun addUpdata(crmFKView : CrmFKView){
        this.updata=crmFKView
    }

    override fun initData() {
        initView()
        initdata()
    }

    private fun initView() {
        mxuanzhongid=ArrayList()
        mjingliid=ArrayList()
//        TvName.setOnClickListener(this)
        BtBaocun.setOnClickListener(this)
//        TvTel.setOnClickListener(this)
//        TvMail.setOnClickListener(this)
//        TvQq.setOnClickListener(this)
//        TvOccupation.setOnClickListener(this)
//        TvKehulaiyuan.setOnClickListener(this)
        BtBaocun.setOnClickListener(this)
        LlXiaoshouyuan.setOnClickListener(this)
        LlStoreManager.setOnClickListener(this)
//        var filters = arrayOf(CharFilter.wnrCharFilter())
//        TvName.filters = filters
//        TvTel.filters = filters
//        TvMail.filters = filters
//        TvQq.filters = filters
//        TvOccupation.filters = filters
//        TvKehulaiyuan.filters = filters

        kehuleibie.add("")
        kehuleibie.add("单身贵族")
        kehuleibie.add("二人世界")
        kehuleibie.add("家有儿女")
        kehuleibie.add("事业有成")
        kehuleibie.add("天伦之乐")
        val adapter = ArrayAdapter(activity, R.layout.lv_sp_tvitem, kehuleibie)
        SpCustomerCategory.adapter = adapter
        SpCustomerCategory.setSelection(1)
        when(code){
            1 -> {
                BtBaocun.text = "确定新增"
            }
        }
    }

    private fun initdata() {
        Log.d("initdata","data="+clientDataBean.toString())
        Myutil.settv(clientDataBean.name, TvName)
        if (clientDataBean.sex == "0") {
            RbNan.isChecked=true
        } else if (clientDataBean.sex == "1") {
            RbNv.isChecked=true
        }
        Myutil.settv(clientDataBean.tel, TvTel)
        Myutil.settv(clientDataBean.wechat_number, TvWeiXing)
//        Myutil.settv(clientDataBean.customer_name,TvExclusive_shop)
        val res = resources
        val kehulaiyuan = res.getStringArray(R.array.kehulaiyuan)
        for(i in kehulaiyuan.indices){
            if(clientDataBean.kehulaiyuan.trim() == kehulaiyuan[i]){
                SpKehulaiyuan.setSelection(i)
            }
        }
        val kehunianling = res.getStringArray(R.array.kehunianling)
        for (i in kehunianling.indices){
            if(clientDataBean.age.trim() == kehunianling[i]){
                SpKehuNianLing.setSelection(i)
            }
        }
        val kehuzhiye = res.getStringArray(R.array.kehuzhiye)
        for (i in kehuzhiye.indices){
            if(clientDataBean.occupation.trim() == kehuzhiye[i]){
                SpOccupation.setSelection(i)
            }
        }
        for (i in kehuleibie.indices){
            if(clientDataBean.customer_cat.trim() == kehuleibie[i]){
                SpCustomerCategory.setSelection(i)
            }
        }
        if (clientDataBean.xiaoshouyuan.size != 0) {
            val xiaoshouyuan = StringBuffer("")
            for (j in 0 until clientDataBean.xiaoshouyuan.size) {
                if (j < clientDataBean.xiaoshouyuan.size - 1) {
                    xiaoshouyuan.append(clientDataBean.xiaoshouyuan[j]).append("\n")
                } else {
                    xiaoshouyuan.append(clientDataBean.xiaoshouyuan[j])

                }
            }
            Myutil.settv(xiaoshouyuan.toString(), TvXiaoshouyuan)
        }
        Myutil.settv(clientDataBean.store_manager_name, TvStoreManager)
        Myutil.setet(clientDataBean.demand, EtDemand)
        Myutil.setet(clientDataBean.remark, EtRemark)
    }

    override fun onClick(v: View?) {
        when (v) {
            TvName -> {
                getintent(0, 1, "客户名", TvName.text.toString())
            }
            TvTel -> {
                getintent(0, 2, "手机号", TvTel.text.toString())
            }
            LlXiaoshouyuan -> {
                val  pop = PopShejiXiaoshouDuoxuan(context!!,PopShejiXiaoshouDuoxuan.XSY,clientDataBean.xiaoshouyuan,object : PopShejiXiaoshouDuoxuan.duoxuan{
                    override fun xuanze(list: MutableList<String>, listid: MutableList<String>) {
                        val shejishi = StringBuffer("")
                        for (j in 0 until list.size) {
                            if (j == list.size - 1) {
                                shejishi.append(list[j])
                            } else {
                                shejishi.append(list[j])
                                shejishi.append("\n")
                            }
                        }
                        Myutil.settv(shejishi.toString(), TvXiaoshouyuan)
                        mxuanzhongid=listid
                    }

                },"请选择销售员")
                pop.show(sp,TvXiaoshouyuan)
            }
            LlStoreManager -> {
                var list : MutableList<String> = ArrayList()
                list.add(clientDataBean.store_manager_name)
                val  pop = PopShejiXiaoshouDuoxuan(context!!,PopShejiXiaoshouDuoxuan.JL,list,object : PopShejiXiaoshouDuoxuan.duoxuan{
                    override fun xuanze(list: MutableList<String>, listid: MutableList<String>) {
                        val shejishi = StringBuffer("")
                        for (j in 0 until list.size) {
                            if (j == list.size - 1) {
                                shejishi.append(list[j])
                            } else {
                                shejishi.append(list[j])
                                shejishi.append("\n")
                            }
                        }
                        Myutil.settv(shejishi.toString(), TvStoreManager)
                        mjingliid=listid
                    }

                },"请选择门店经理")
                pop.dan(true)
                pop.show(sp,TvStoreManager)
            }
            BtBaocun -> {
                when(code){
                    1 -> {
                        updata.updata("gengx")
                    }
                    2-> {
                        if(!Myutil.isTel(TvTel.text.toString())){
                            Myutil.MyTost("输入正确的电话号码")
                            return
                        }
                        val jo=xiugai()
                        Log.d("TAAG","jo="+jo.toString())
                        updatedialog= Myutil.showupdatedialog(context, "正在保存", false)
                        Httputil.cleanclient(activity, ClientKActivity.getid(),jo,object : OnUserDataInterface {
                            override fun onUserData(data: String?) {
                                Myutil.MyTost("客户资料上传成功")
                                Myutil.longlogshow("TAAG","jdata="+Myutil.decodeUnicode(data))
                                updatedialog.dismiss()
                                if(data==null){
                                    return
                                }
                                FKP.updata(data)
                            }

                            override fun onNot(throwable: String?) {
                                updatedialog.dismiss()
                                Log.d("TAAG","throwable="+throwable)
                            }

                        })
                    }
                }
            }
        }
    }

    private fun xiugai(): JSONObject {
        var jo = JSONObject()
        try {
            val customer = JSONObject()
//            customer.put("exclusive_shop",TvExclusive_shop.text.toString())
            customer.put("name", TvName.text.toString())
            customer.put("tel", TvTel.text.toString())
            customer.put("wechat_number", TvWeiXing.text.toString())
            customer.put("occupation", SpOccupation.selectedItem.toString())
            customer.put("kehulaiyuan", SpKehulaiyuan.selectedItem.toString())
            customer.put("customer_cat", SpCustomerCategory.selectedItem.toString())
            customer.put("age", SpKehuNianLing.selectedItem.toString())
            customer.put("demand", EtDemand.text.toString())
            customer.put("remark", EtRemark.text.toString())
            if(mjingliid.size>0){
                customer.put("store_manager", mjingliid[0])
            }
            if(TvXiaoshouyuan.text.toString()!=""&&mxuanzhongid.size>0) {
                val js = JSONArray()
                for (s in mxuanzhongid) {
                    js.put(s)
                }
                customer.put("xiaoshouyuan", js)
            }
            if(RbNan.isChecked){
                customer.put("sex", "0")
            }else if(RbNv.isChecked){
                customer.put("sex", "1")
            }
            jo.put("customer", customer)
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("TAAAG", "--------------" + e.toString())
        }
        return jo
    }

    fun notifydata(clientDataBean : ClientDataBean) {
        this.clientDataBean=clientDataBean
        initdata()
    }


    fun getBean() : ClientDataBean{
        clientDataBean.name=TvName.text.toString()
        clientDataBean.tel=TvTel.text.toString()
        clientDataBean.wechat_number=TvWeiXing.text.toString()
        clientDataBean.age=SpKehuNianLing.selectedItem.toString()
        clientDataBean.occupation=SpOccupation.selectedItem.toString()
        clientDataBean.kehulaiyuan=SpKehulaiyuan.selectedItem.toString()
        clientDataBean.demand=EtDemand.text.toString()
        clientDataBean.remark=EtRemark.text.toString()

        clientDataBean.customer_cat=SpCustomerCategory.selectedItem.toString()
//        clientDataBean.exclusive_shop=TvExclusive_shop.text.toString()
        clientDataBean.xiaoshouyuanid=mxuanzhongid
        if(mjingliid.size>0) {
            clientDataBean.store_manager_nameid = mjingliid[0]
        }
        if(RbNan.isChecked){
            clientDataBean.sex="0"
        }else if(RbNv.isChecked){
            clientDataBean.sex="1"
        }
        return clientDataBean
    }
    private fun getintent(select: Int, requestCode: Int, title: String, data: String) {
        val intent = Intent()
        var a = activity;
        if(a != null){
            intent.setClass(a, SeekActivity::class.java)
        }
        intent.putExtra("select", select)
        intent.putExtra("title", title)
        intent.putExtra("data", data)
        startActivityForResult(intent, requestCode)
        activity!!.overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null){
            return
        }
        if (resultCode == Activity.RESULT_OK) {
//            when (requestCode) {
//                1 -> {
//                    val s = data.getStringExtra("name").trim()
//                    TvName.text = s
//                }
//                2 -> {
//                    val s=data.getStringExtra("name").trim()
//                    TvTel.text=s
//                }
//                3 -> {
//                    val s=data.getStringExtra("name").trim()
//                    TvQq.text=s
//                }
//                4 -> {
//                    val s=data.getStringExtra("name").trim()
//                    TvMail.text=s
//                }
//                5 -> {
//                    val s=data.getStringExtra("name").trim()
//                    TvOccupation.text=s
//                }
//                6 -> {
//                    val s=data.getStringExtra("name").trim()
//                    TvKehulaiyuan.text=s
//                }
//            }
        }
    }
}
